package imp.core.data;

import imp.core.basics.ErrorHandler;
import imp.core.basics.IOManager;
import imp.core.data.configuration.FilePaths;
import imp.core.debug.DebugManager;


/**
 * This class contains the program data.
 * @author Patrick Arnold
 * @since 09.09.2010
 *
 */
public class SettingsData implements FilePaths {
	
	/** The sort key which specifies how to sort the music list. */
	private static int sortKey;
	
	/** Specifies the time format (0: sss, 1: mm:ss). */
	private static int timeFormat;
	
	/** Specifies whether tool tips should be displayed (0 no, 1 important ones, 2 all). */
	private static int toolTips;
		
	/** The look and feel (0 Java, 1 Windows). */
	private static int lookAndFeel;	
	
	/** The window size of the main menu (0 1024x768, 1 1152x854, 2 1200x800, 3 full screen). */
	private static int windowSize;
	
	/** The threshold that specifies how many percent must be played to be considered as played. */
	private static int played_threshold_percentage;	
	
	/** The threshold that specifies how long a song must be played to be considered as played. */
	private static int played_threshold_seconds;
	
	
	
	/**
	 * Loads the setting data.
	 * @return Specifies whether loading the setting data was successful or not.
	 */
	public static boolean loadData() {
		
		String path = PathData.getProgrampath() + SETTINGS;
		String[] file = IOManager.loadFile( path);
		
		if( file == null) {
			return false;
		}
		
		if( file.length < 7) {
			
			String message = "Incorrect number of lines in settings.dat: <br /> &nbsp &nbsp &nbsp "
				+ "Expected 7, found " + file.length;
			String location = ErrorHandler.getParserLocation();
			ErrorHandler.error( message, location, null);
		
		}
		
		
		try {
    	
			sortKey = Integer.parseInt( file[0]);
    		timeFormat = Integer.parseInt( file[1]);
    		toolTips = Integer.parseInt( file[2]);
    		lookAndFeel = Integer.parseInt( file[3]);
    		windowSize = Integer.parseInt( file[4]);	
    		played_threshold_percentage = Integer.parseInt( file[5]);
    		played_threshold_seconds = Integer.parseInt(  file[6]);
    		
    		if( SessionData.isDebugMode()) {
    			
    			String[] output = new String[7];
    			
    			output[0] = "Sort Key:             " + sortKey;
    			output[1] = "Time format:          " + timeFormat;
    			output[2] = "Tool tios:            " + toolTips;
    			output[3] = "Look & feel:          " + lookAndFeel;
    			output[4] = "Window size:          " + windowSize;
    			output[5] = "Threshold (perc.):    " + played_threshold_percentage;
    			output[6] = "Threshold (sec.):     " + played_threshold_seconds;
    			
    			DebugManager.displayStringField( output, "Settings Data");
    			
    		}
    		
    		return true;
		
		} catch( Exception e) {
			
			String message = "Incorrect line in settings.dat";
			String location = ErrorHandler.getParserLocation();
			ErrorHandler.error( message, location, null);
			
			return false;
			
		}
		
	}
	
	
	
	/**
	 * Writes the settings file after the settings were changed by the user.
	 * @return Specifies whether writing the settings has been successful or not.
	 */
	public static boolean writeData() {
		
		String path = PathData.getProgrampath() + "prog//settings.dat";
		String[] output = new String[8];

		output[0] = "" + sortKey;
		output[1] = "" + timeFormat;
		output[2] = "" + toolTips;
		output[3] = "" + lookAndFeel;
		output[4] = "" + windowSize;
		output[5] = "" + played_threshold_percentage;
		output[6] = "" + played_threshold_seconds;
		
		boolean re = IOManager.write( output, path, false);
		return re;
			
	}
	
	
	/**
	 * Returns the sort key which specifies how the music list is to be sorted.
	 * @return The sort key for the music list.
	 */
	public static int getSortKey() {
		return sortKey;
	}
	
	
	/**
	 * Sets the sort key which specifies how the music list is to be sorted.
	 * @param sortKey The sort key to set.
	 */
	public static void setSortKey( int sortKey) {
		SettingsData.sortKey = sortKey;
	}
	
	
	
	/**
	 * Returns the time format.
	 * @return The timeFormat (0: sss, 1: mm:ss).
	 */
	public static int getTimeFormat() {
		return timeFormat;
	}

	
	/**
	 * Sets the time format.
	 * @param timeFormat The time format to set.
	 */
	public static void setTimeFormat( int timeFormat) {
		SettingsData.timeFormat = timeFormat;
	}
	

	
	/**
	 * Returns whether the tool tips should be generally displayed (2), only important tool tips
	 * should be displayed (1) or no tool tips should be displayed (0).
	 * @return Specifies whether tool tips should be displayed or not.
	 */
	public static int getToolTips() {
		return toolTips;
	}

	
	/**
	 * Sets the tool tip value which specifies whether tool tips should be generally displayed (2), 
	 * only important tool tips should be displayed (1) or no tool tips should be displayed (0).
	 * @param toolTips The tool tips displaying settings to set.
	 */
	public static void setToolTips( int toolTips) {
		SettingsData.toolTips = toolTips;
	}

	
	/**
	 * Returns the look & feel. (0 Java, 1 Windows).
	 * @return The current look & feel.
	 */
	public static int getLookAndFeel() {
		return lookAndFeel;
	}
	
	
	/**
	 * Sets the look and feel for the GUI.
	 * @param lookAndFeel The look and feel (0 Java, 1 Windows).
	 */
	public static void setLookAndFeel( int lookAndFeel) {
		SettingsData.lookAndFeel = lookAndFeel;
	}	
	
	
	
	/**
	 * Returns the default window size (0 1024x768, 1 1152x854, 2 1200x800, 3 full screen).
	 * @return The default windows size.
	 */
	public static int getWindowSize() {
		return windowSize;
	}
	
	
	/**
	 * Sets the default window size (0 1024x768, 1 1152x854, 2 1200x800, 3 full screen).
	 * @param windowSize The window size.
	 */
	public static void setWindowSize( int windowSize) {
		SettingsData.windowSize = windowSize;
	}	
	
	
	/**
	 * Sets the threshold which specifies when a song will be considered as played (percentage).
	 * @param log_threshold_percentage Specifies how long a song must be played (percentage)
	 * to be considered as played.
	 */
	public static void setPlayed_threshold_percentage( int log_threshold_percentage) {
		SettingsData.played_threshold_percentage = log_threshold_percentage;
	}	
	
	
	/**
	 * Gets the threshold which specifies when a song will be considered as played (percentage).
	 * @return The threshold which specifies when a song will be considered as played (percentage).
	 */
	public static int getPlayed_threshold_percentage() {
		return SettingsData.played_threshold_percentage;
	}	
	
	
	/**
	 * Sets the threshold which specifies when a song will be considered as played (seconds).
	 * @param log_threshold_percentage Specifies how long a song must be played (seconds)
	 * to be considered as played.
	 */
	public static void setPlayed_threshold_seconds( int log_threshold_percentage) {
		SettingsData.played_threshold_seconds = log_threshold_percentage;
	}	
	
	
	/**
	 * Gets the threshold which specifies when a song will be considered as played (percentage).
	 * @return The threshold which specifies when a song will be considered as played (percentage).
	 */
	public static int getPlayed_threshold_seconds() {
		return SettingsData.played_threshold_seconds;
	}
	
	
}